<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'
import { queryTodayTrainWordWeekData } from '@/api/statistics'

const animationDuration = 1000

const weekMap = {
  周日: 'sunday',
  周一: 'monday',
  周二: 'tuesday',
  周三: 'wednesday',
  周四: 'thursday',
  周五: 'friday',
  周六: 'saturday'
}

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '218px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.drawChart()
      // queryTodayTrainWordWeekData().then(res => {
      //   this.drawChart(res.data)
      // })
    },
    drawChart() {
      let day = new Date().getDay()
      let weeks = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
      let xAxisWeek = []
      xAxisWeek.push(...weeks.slice(day))
      xAxisWeek.push(...weeks.slice(0, day))

      let autoData = [10, 20, 44, 32, 56, 14, 9]
      let manualData = [19, 22, 42, 31, 66, 45, 6]
      // let autoData = xAxisWeek.map(d => {
      //   let key = `${weekMap[d]}Count`
      //   return data[key] || 0
      // })
      // let manualData = xAxisWeek.map(d => {
      //   let key = `${weekMap[d]}ManualCount`
      //   return data[key] || 0
      // })
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            // type: "shadow" // 默认为直线，可选为：'line' | 'shadow'
            lineStyle: {
              color: '#fff3'
            }
          }
        },
        grid: {
          top: 35,
          left: '3%',
          right: '5%',
          bottom: '3%',
          containLabel: true
        },
        legend: {
          data: ['机车预警', '人员预警'],
          textStyle: {
            color: '#fff'
          }
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: xAxisWeek,
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisTick: {
              show: false
            },
            max: function(value) {
              return Math.ceil(value.max * 1.5)
            }
          }
        ],
        series: [
          {
            name: '机车预警',
            type: 'line',
            barWidth: '40%',
            data: manualData,
            barGap: '0',
            animationDuration,
            lineStyle: {
              width: 2
            },
            areaStyle: {
              opacity: 0.2
            },
            showSymbol: false
          },
          {
            name: '人员预警',
            type: 'line',
            barWidth: '40%',
            data: autoData,
            animationDuration,
            lineStyle: {
              width: 2
            },
            areaStyle: {
              opacity: 0.2
            },
            showSymbol: false
          }
        ]
      })
    }
  }
}
</script>
